<?php

namespace app\api\controller;

use app\api\model\Jobfair;
use app\api\model\JobfairEnroll;
use app\api\model\JobfairPosition;
use app\api\model\Subject;
use app\api\model\SubjectCompany;
use app\api\model\SubjectPersonal;

class Zhao extends Common
{
    public function getList()
    {
        $limit  = $this->request->get('limit', 20, 'intval');
        $page   = $this->request->get('page', 1, 'intval');
        $type = $this->request->get('type','jobfair'); //jobfair 线下招聘会  subject 线上招聘会
        if($type == "jobfair"){
            $data =  Jobfair::where(['display' => 1])->order('id desc')->field("id,title,holddate_start,holddate_end,address,click,thumb")->page($page,$limit)->select();
            $count = Jobfair::where(['display' => 1])->count();
        }else{
            $data =  Subject::where(['is_display' => 1])->order('id desc')->field('id,title,small_img as thumb,holddate_start,holddate_end,area as address,click')->page($page,$limit)->select();
            $count = Subject::where(['is_display' => 1])->count();
        }
        foreach ($data as &$v){
            $v['type'] = $type;
        }
        return $this->ajaxReturn($this->successCode,'返回成功',htmlOutList(['list' => $data,'count' => $count]));
    }

    public function detail()
    {
        $uid = intval($this->request->uid);
        $type = $this->request->get('type','jobfair');
        $id  = $this->request->get('id', 0, 'intval');
        if($type == "jobfair"){
            Jobfair::where(['id' => $id])->inc('click', 1)->update();
            $data = Jobfair::where(['id' => $id])->field("id,title,holddate_start,holddate_end,address,click,thumb,contact,phone,map_x,map_y,introduction")->find()->toArray();
            $data['company'] = JobfairPosition::where(['jobfair_id' => $id])->alias('a')->join('company_profile b','a.company_id = b.id','left')->join('company_img c','a.company_id = c.company_id','left')->field('companyname,trade_cn,img')->group("b.id")->select();
            $data['is_apply'] = JobfairEnroll::where(['uid' => $uid,'jobfair_id' => $id])->count();
        }else{
            Subject::where(['id' => $id])->inc('click', 1)->update();
            $data = subject::where(['id' => $id])->field("id,title,small_img as thumb,holddate_start,holddate_end,area as address,click,content as introduction")->find()->toArray();
            //,contact,phone,map_x,map_y
            $data['contact'] = '';
            $data['phone'] = '';
            $data['map_x'] = '';
            $data['map_y'] = '';
            $data['company'] = SubjectCompany::where(['subject_id' => $id])->alias('a')->join('company_profile b','a.company_uid = b.id','left')->join('company_img c','b.id = c.company_id','left')->field('companyname,trade_cn,img')->group("b.id")->select();
            $data['is_apply'] = SubjectPersonal::where(['resume_uid' => $uid,'subject_id' => $id])->count();
        }
        return $this->ajaxReturn($this->successCode,'返回成功',htmlOutList($data));
    }

    public function apply()
    {
        $uid = intval($this->request->uid);
        $type = $this->request->post('type','jobfair');
        $id  = $this->request->post('id', 0, 'intval');
        if($type == "jobfair"){
            $data['is_apply'] = JobfairEnroll::where(['uid' => $uid,'jobfair_id' => $id])->count();
            if($data['is_apply']){
                return $this->ajaxReturn($this->errorCode,'你已经报过名了',htmlOutList($data));
            }
            $result = JobfairEnroll::insert(
                [
                    'uid' => $uid,
                    'jobfair_id' => $id,
                    'status' => 0,
                    'create_time' => time()
                ]
            );
        }else{
            $data['is_apply'] = SubjectPersonal::where(['resume_uid' => $uid,'subject_id' => $id])->count();
            if($data['is_apply']){
                return $this->ajaxReturn($this->errorCode,'你已经报过名了',htmlOutList($data));
            }
            $result = SubjectPersonal::insert(
                [
                    'resume_uid' => $uid,
                    'subject_id' => $id,
                    'robot' => 0,
                    'addtime' => time(),
                    's_audit' => 1
                ]
            );
        }
        if($result){
            return $this->ajaxReturn($this->successCode,'提交成功',htmlOutList([]));
        }
        return $this->ajaxReturn($this->errorCode,'提交失败',htmlOutList([]));
    }
}